package YYX.Smcs;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * @ClassName: AddView
 * @Auther: Drawin
 * @Date: 2021/12/26 19:52
 * @Description:添加，修改功能
 */

public class AddView extends JFrame {
    private JPanel jPanelCenter, jPanelSouth;
    private JButton addButton, exitButton,modiButton;
    private JTextField stuID,name,sex,age,callnumber,home_place,identityID;
    Function act =new Function();

    public AddView(){
        //center
        jPanelCenter = new JPanel();
        jPanelCenter.setLayout(new GridLayout(7, 2));

        jPanelCenter.add(new JLabel("  学号"));
        stuID=new JTextField();
        jPanelCenter.add(stuID);
        jPanelCenter.add(new JLabel("  姓名"));
        name = new JTextField();
        jPanelCenter.add(name);
        jPanelCenter.add(new JLabel("  性别"));
        sex = new JTextField();
        jPanelCenter.add(sex);
        jPanelCenter.add(new JLabel("  年龄"));
        age = new JTextField();
        jPanelCenter.add(age);
        jPanelCenter.add(new JLabel("  电话号码"));
        callnumber = new JTextField();
        jPanelCenter.add(callnumber);
        jPanelCenter.add(new JLabel("  家庭住址"));
        home_place = new JTextField();
        jPanelCenter.add(home_place);
        jPanelCenter.add(new JLabel("  身份证号码"));
        identityID = new JTextField();
        jPanelCenter.add(identityID);

        //south
        jPanelSouth = new JPanel();
        jPanelSouth.setLayout(new GridLayout(1, 3));
        addButton=new JButton("添加");
        exitButton=new JButton("返回");
        modiButton=new JButton("修改");
        jPanelSouth.add(addButton);
        jPanelSouth.add(modiButton);
        jPanelSouth.add(exitButton);

        this.add(jPanelCenter, BorderLayout.CENTER);
        this.add(jPanelSouth, BorderLayout.SOUTH);

        //监听
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
               new MainView();
            }
        });
        Student s1=new Student();
        ArrayList<Student> arry=new ArrayList<Student>();
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sID = stuID.getText();    //获取文本框中内容
                if(act.seek(sID)!=-1)
                {
                    JOptionPane.showMessageDialog(null, "该学号对应的学生已经存在！");
                    //清空文本框
                    stuID.setText("");
                    name.setText("");
                    age.setText("");
                    callnumber.setText("");
                    home_place.setText("");
                    identityID.setText("");
                    return;
                }
                String sname = name.getText();
                //先判断学号和姓名是否为空，如果为空，直接退出
                if(sID.equals("")||sname.equals(""))
                {
                    JOptionPane.showMessageDialog(null, "请重新输入");
                    return;
                }
                String sage = age.getText();
                if(sage.equals(""))//年龄为空，没有输入
                    sage="--";
                String scall = callnumber.getText();
                if(scall.equals(""))
                    scall="--";
                String shome = home_place.getText();
                if(shome.equals(""))
                    shome="--";
                String siden = identityID.getText();
                if(siden.equals(""))
                    siden="--";
                String ssex=sex.getText();
                if (ssex.equals(""))
                    ssex="--";


                s1.setStuID(sID);
                s1.setAge(sage);
                s1.setName(sname);
                s1.setSex(ssex);
                s1.setCallnummber(scall);
                s1.setHome_place(shome);
                s1.setIdentityID(siden);

                System.out.println(s1.fileString());
                act.add(s1);
                act.writefile();

                JOptionPane.showMessageDialog(null, "录入成功！！！");
                setVisible(false);
                new MainView();

            }
        });

        modiButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sID =stuID.getText();    //获取文本框中内容
                int flag=act.seek(sID);    //查找是否存在
                if (flag==-1)
                {
                    JOptionPane.showMessageDialog(null, "未查找到该学号的学生！！！\n\n请重新输入");
                    return;
                }
                else {
                    JOptionPane.showMessageDialog(null, "确认返回后请输入需要修改后的数据");
                }
                String sage = age.getText();
                if(sage.equals(""))//年龄为空，没有输入
                    sage="--";
                String scall = callnumber.getText();
                if(scall.equals(""))
                    scall="--";
                String shome = home_place.getText();
                if(shome.equals(""))
                    shome="--";
                String siden = identityID.getText();
                if(siden.equals(""))
                    siden="--";
                String ssex=sex.getText();
                if (ssex.equals(""))
                    ssex="--";

                String sname = name.getText();
                //先判断学号和姓名是否为空，如果为空，直接退出
                if(sname.equals(""))
                {
                    JOptionPane.showMessageDialog(null, "录入的学生姓名为空！！！\n\n请重新输入");
                    return;
                }
                s1.setStuID(sID);
                s1.setAge(sage);
                s1.setName(sname);
                s1.setSex(ssex);
                s1.setCallnummber(scall);
                s1.setHome_place(shome);
                s1.setIdentityID(siden);

                act.modify(s1);       //修改
                act.writefile();
                JOptionPane.showMessageDialog(null, "修改成功！！！");
            }
        });

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(500, 300, 400, 270);
        setVisible(true);

    }
}
